﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using System.Net;
using System.IO;
using System.Web;
using WFNetLib.Net;
using WFNetLib.GSMModem;
using System.IO.Ports;

namespace ATReaderTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        AT指令 at;
        //PPPProc pppProc;
        private void serialPort1_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
        {
            MessageBox.Show(e.EventType.ToString());
        }        

        private void Form1_Load(object sender, EventArgs e)
        {
            at = new AT指令(serialPort1);
            //pppProc = new PPPProc(serialPort1);
        }
        bool AddATLog(string str1, string strEnd,int rt)
        {
            char[] MyChar = { '\r', '\n' };
            textBox2.AppendText("PC->GPRS:  " + str1 + "\r\n");
            string str2 = ""; 
            return at.ATSend(str1,strEnd,rt);
            str2 = str2.TrimStart(MyChar);
            str2 = str2.TrimEnd(MyChar);
            textBox2.AppendText("GPRS->PC:  " + str2 + "\r\n");
            StringBuilder sb = new StringBuilder();
            byte[] bs = Encoding.UTF8.GetBytes(str2);
            string str = "";
            foreach (byte b in bs)
            {
                str += b.ToString("X2") + " ";
            }
            textBox2.AppendText(str + "\r\n");
            textBox2.AppendText("-----------------------------------------------------------\r\n");
        }
        bool AddATLog(string str1,int rt)
        {
            return AddATLog(str1, "OK",rt);            
        }
        private void button1_Click(object sender, EventArgs e)
        {
//             try
//             {
                serialPort1.Open();
                serialPort1.RtsEnable = true;
                AddATLog("ATE0",3);
                AddATLog("ATI",3);
                AddATLog("AT+CGSN",3);
                AddATLog("AT+CSQ", 3);
                AddATLog("AT+CNMI=2,1,0,0", 3);
                AddATLog("AT+CSCA?", 3);//+CSCA: "+8613800451500",145,短信服务中心地址应该为13800451500，若不是则修改为这个
                AddATLog("AT+CIMI", 3);//查询手机卡的IMSI（国际移动台设备标识）
                AddATLog("AT+CCID?", 3);//获得SIM的序列号，华为的好像不支持，没有这个命令，wavecomm的有
                AddATLog("AT+CREG?", 3);//查询是否已经注册上网络,+CREG: 0,1(其中的1表示已注册，本地网，若为0则应该等待其变为1)
                AddATLog("AT+CLIP=1", 3);//来电显示
                AddATLog("AT+CMGF=0", 3);//SMS 格式,0为PDU
                AddATLog("AT+CPMS=\"SM\",\"SM\"", 3);//优选消息存储器,将消息保存的sim卡中
                AddATLog("AT+CGDCONT=1,\"IP\",\"CMWAP\"", 3);//定义PDP 上下文
                AddATLog("AT+CGREG?", 3);//GPRS 网络注册状态,+CGREG: 0,1(其中的1表示已注册，本地网，若为0则应该等待其变为1)
                AddATLog("AT+CGAUTO=1",3);
                AddATLog("AT",3);
                AddATLog("ATE0",3);
                AddATLog("ATD*99***1#","CONNECT",3);//拨号
                //pppProc.PPPConnect();
//             }
//             catch (System.Exception ex)
//             {
//                 MessageBox.Show(ex.Message);
//             }
            serialPort1.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            //serialPort1.RtsEnable = true;
            AddATLog("ATE0", 3);
            AddATLog("ATI", 3);
            AddATLog("ATZ1", 3);
            //AddATLog("AT%IPCLOSE=5", 3);
            AddATLog("AT+CGDCONT=1,\"IP\",\"CMWAP\"", 3);
            AddATLog("AT+CGREG?", 3);
            AddATLog("AT+CGACT=1,1", 3);
            AddATLog("AT%ETCPIP", 3);
            AddATLog("AT%IPOPEN=\"TCP\",\"10.0.0.172\",80", "CONNECT", 3);
            AddATLog("AT%IOMODE=1,1,1", 3);
            StringBuilder sb = new StringBuilder();
            sb.Append("GET http://wap.cmread.com/iread/wml/l/readbook.jsp?bid=364811455&cid=364811457&cm=84377 HTTP/1.1" + "\r\n");
            sb.Append("User-Agent: NokiaN82/1.0 (20.1.062) SymbianOS/9.2 Series60/3.1 Profile/MIDP-2.0 Configuration/CLDC-1.1" + "\r\n");
            sb.Append("Host: wap.cmread.com" + "\r\n");
            sb.Append("Accept: */*;q=0.1" + "\r\n");
            sb.Append("Accept-Language: en-US,en;q=0.9" + "\r\n");
            sb.Append("Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1" + "\r\n");
            sb.Append("Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0" + "\r\n");
            sb.Append("Proxy-Connection: Keep-Alive" + "\r\n");
            sb.Append("\r\n"+"\"");
            byte[] tx=Encoding.ASCII.GetBytes(sb.ToString());
            StringBuilder sb1 = new StringBuilder();
            for(int i=0;i<tx.Length;i++)
            {
                sb1.Append(tx[i].ToString("X2"));
                if(sb1.Length>1022)
                {
                    string attx="AT%IPSEND=\""+sb1.ToString()+"\"";
                    AddATLog(attx.ToString(), 3);
                    sb1.Clear();
                }
            }
            if (sb1.Length != 0)
            {
                string attx = "AT%IPSEND=\"" + sb1.ToString() + "\"";
                AddATLog(attx, 3);
                //sb1.Clear();
            }
            AddATLog("AT%IPSEND?", 3);
//                 if(at.RxList[0]=="%IPSEND:0")
//                     break;
            while(true)
            AddATLog("AT%IPDQ",3);
            

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            serialPort1.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SerialPort com = new SerialPort("COM4", 115200);
            com.Handshake = Handshake.RequestToSend;
            com.DtrEnable = true;  
            SMSThread read = new SMSThread(com);
            read.ReaderThreadStart();
        }
        void dsfd(ref string str)
        {
            str = "213";
        }
    }
}
